package com.yd.mvc.pc.dicController;

import com.yd.app.service.common.LoggerService;
import com.yd.app.service.pc.dicService.DicService;
import com.yd.domain.business.GType;
import com.yd.domain.commom.PageModel;
import com.yd.domain.sys.Dic;
import com.yd.domain.searchParam.DicParam;
import com.yd.domain.sys.Logger;
import com.yd.mvc.baseController.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @author zy
 * @create 2018-03-07 17:40
 * @desc
 **/
@RestController
@RequestMapping(value = "/api/dic")
public class DicController extends BaseController{
    @Autowired
    private DicService dicService;
    @Autowired
    private LoggerService loggerService;
    /**
     * @Author: zy
     * @Desc: 新增数据字典
     * @Date:  2018/3/7
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public void addDic(@RequestBody Dic dic,HttpServletRequest request)  {
        String user="admin";
        String suff="字典ID"+dic.getId()+",字典code:"+dic.getCode()+";字典名称:"+dic.getTitle()+";字典类型:"+dic.getType();
        try {
            dicService.addDic(dic);
            loggerService.addLogger(new Logger("新增字典",user,Logger.LOG_TYPE.ADD,Logger.STATUS.SUCCESS,Logger.MODEL_TYPE.DIC,"",suff),request);
        } catch (Exception e) {
            loggerService.addLogger(new Logger("新增字典",user,Logger.LOG_TYPE.ADD,Logger.STATUS.FAIL,Logger.MODEL_TYPE.DIC,"",suff),request);
            e.printStackTrace();
        }
    }
    /**
     * @Author: zy
     * @Desc:查询数据字典列表
     * @Date:  2018/3/7
     */
    @RequestMapping(value = "/list/{pageIndex}/{pageSize}", method = RequestMethod.POST)
    public PageModel getDicList(@RequestBody DicParam dicParam, @PathVariable("pageIndex")int pageIndex, @PathVariable("pageSize")int pageSize){
        PageModel pm=dicService.getDicList(dicParam,pageIndex,pageSize);
        return pm;
    }
    /**
     * @Author: zy
     * @Desc: 根据类型获取数据字典
     * @Date:  2018/3/8
     */
    @RequestMapping(value = "/getDicCacheByType/{dicType}",method = RequestMethod.GET)
    public List<Dic> getDicCacheByType(@PathVariable("dicType") String dicType){
        List<Dic> list =new ArrayList<>();
        list=dicService.getDicCacheByType(dicType);
        return list;
    }
    /**
     * @Author: zy
     * @Desc: 数据字典的修改
     * @Date:  2018/3/8
     */
    @RequestMapping(value = "/update", method = RequestMethod.PUT)
    public void updateDic(@RequestBody Dic dic,HttpServletRequest request){
        String user="admin";
        String suff="字典ID"+dic.getId()+",字典code:"+dic.getCode()+";字典名称:"+dic.getTitle()+";字典类型:"+dic.getType();
        try {
            dicService.updateDic(dic);
            loggerService.addLogger(new Logger("修改字典",user,Logger.LOG_TYPE.UPDATE,Logger.STATUS.SUCCESS,Logger.MODEL_TYPE.DIC,"",suff),request);
        } catch (Exception e) {
            loggerService.addLogger(new Logger("修改字典",user,Logger.LOG_TYPE.UPDATE,Logger.STATUS.FAIL,Logger.MODEL_TYPE.DIC,"",suff),request);
            e.printStackTrace();
        }
    }
    /**
     * @Author: zy
     * @Desc: 删除数据字典
     * @Date:  2018/3/8
     * @Param
     */
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.DELETE)
    public void deleteDic(@PathVariable("id") String id,HttpServletRequest request){
        String user="admin";
        String suff="字典ID:"+id;
        try {
            dicService.deleteDic(id);
            loggerService.addLogger(new Logger("删除字典",user,Logger.LOG_TYPE.DELETE,Logger.STATUS.SUCCESS,Logger.MODEL_TYPE.DIC,"",suff),request);
        } catch (Exception e) {
            e.printStackTrace();
            loggerService.addLogger(new Logger("删除字典",user,Logger.LOG_TYPE.DELETE,Logger.STATUS.FAIL,Logger.MODEL_TYPE.DIC,"",suff),request);
        }
    }

    /**
     *获取材质类型价格列表
     * @return
     */
    @RequestMapping(value = "/get/gtype",method = RequestMethod.GET)
    public List<GType> getGType(){
        List<GType> list =new ArrayList<>();
        list=dicService.getGTypeList();
        return list;
    }
    /**
     *启用停用gtype
     * @return
     */
    @RequestMapping(value = "/enable/gtype/{id}/{ifUse}",method = RequestMethod.GET)
    public void enableGType(@PathVariable("id")String id,@PathVariable("ifUse")boolean ifUse){
        dicService.enableGType(id,ifUse);
    }
    /**
     *添加gtype
     * @return
     */
    @RequestMapping(value = "/add/gtype",method = RequestMethod.POST)
    public void addGType(@RequestBody GType gType ){
        dicService.addGType(gType);
    }
    /**
     *添加gtype
     * @return
     */
    @RequestMapping(value = "/update/gtype",method = RequestMethod.PUT)
    public void updateGType(@RequestBody GType gType ){
        dicService.updateGType(gType);
    }

}
